/*  mudmap - A tool to create and organize user side maps for MUD games
 *  Copyright (C) 2012  Neop (aka moemneop, email: mneop@web.de)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

config::config(string fi, char sep){
    read(fi, sep);
}

config::~config(){}

void config::read(string fi, char sep){
    separator = sep;
    read(fi);
}

void config::read(string fi){
    if(fi != ""){
        file = fi;
        vector<string> lines = readfile_sv(file, true);
        for(unsigned i = 0; i < lines.size(); i++){
            if(lines[i][0] == '#' || (lines[i][0] == '/' && lines[i][1] == '/')) continue;
            string key = lines[i].substr(0, lines[i].find(separator));
            config_lines[key] = lines[i].substr(lines[i].find(separator) + 1);
            while(config_lines[key][0] == ' ' || config_lines[key][0] == '\t') config_lines[key].erase(1);
        }
    }
}

void config::write(string file){
    if(file != "") this->file = file;
    if(this->file != ""){
        std::ofstream ofs(this->file, std::ios::trunc);
        for(map<string, string>::iterator it = config_lines.begin(); it != config_lines.end(); it++){
            ofs << it->first << separator << it->second << endl;
        }
    }
}

bool config::get_bool(string key, bool def){
    if(config_lines.count(key) != 0 && config_lines[key] != ""){
        if(config_lines[key][0] == 't' || config_lines[key][0] == 'y' || config_lines[key][0] == '1'){
            return true;
        } else if(config_lines[key][0] == 'f' || config_lines[key][0] == 'n' || config_lines[key][0] == '0'){
            return false;
        }
    }
    return def;
}

int config::get_int(string key, int def){
    if(config_lines.count(key) != 0 && config_lines[key] != ""){
        return atoi(config_lines[key].c_str());
    }
    return def;
}

float config::get_float(string key, float def){
    if(config_lines.count(key) != 0 && config_lines[key] != ""){
        return atof(config_lines[key].c_str());
    }
    return def;
}

char config::get_char(string key, char def){
    if(config_lines.count(key) != 0 && config_lines[key] != "") return config_lines[key][0];
    return def;
}

string config::get_string(string key, string def){
    if(config_lines.count(key) != 0 && config_lines[key] != "") return config_lines[key];
    return def;
}

void config::set_bool(string key, bool data){
    config_lines[key] = data ? "t" : "f";
}

void config::set(string key, int data){
    stringstream ss;
    ss << data;
    config_lines[key] = ss.str();
}

void config::set(string key, float data){
    stringstream ss;
    ss << data;
    config_lines[key] = ss.str();
}

void config::set(string key, double data){
    stringstream ss;
    ss << data;
    config_lines[key] = ss.str();
}

void config::set(string key, char data){
    stringstream ss;
    ss << data;
    config_lines[key] = ss.str();
}

void config::set(string key, string data){
    config_lines[key] = data;
}
